package judesart.app.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;

import com.allen_sauer.gwt.log.client.Log;

@Entity
@NamedQueries( {
        @NamedQuery(name = "User.findFeatured", query = "select o from User o where o.featured = true"),
        @NamedQuery(name = "User.findByUsername", query = "select o from User o where o.username = :username"),
        @NamedQuery(name = "User.findAll", query = "select o from User o") })
public class User implements Serializable
{

    private static final long serialVersionUID = 1L;
    @Basic
    @Email
    @NotNull
    private String email;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Basic
    @Size(min = 5, max = 256)
    @NotNull
    private String name;
    @Basic
    @NotNull
    @Size(min = 5, max = 20)
    private String password;
    @Basic
    @NotNull
    @Size(min = 4, max = 20)
    private String username;
    @Basic
    private Boolean featured;

    public Boolean getFeatured() {
		return featured;
	}

	public void setFeatured(Boolean featured) {
		this.featured = featured;
	}

	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    @Size(min = 1, max = 3)
    private Set<UserRole> userRoles = new HashSet<UserRole>();

    public void addRole(Role role)
    {
        UserRole userRole = new UserRole();
        userRole.setRole(role);
        addRole(userRole);
    }

    public void addRole(UserRole role)
    {
        if (userRoles == null)
        {
            userRoles = new HashSet<UserRole>();
        }
        userRoles.add(role);
    }

    public void removeRole(Role role)
    {
        Log.debug("> removeRole : "
                + (getUserRoles() != null ? getUserRoles().size() : "null"));
        if (getUserRoles() != null)
        {
            for (UserRole userRole : getUserRoles())
            {
                if (userRole.getRole() == role)
                {
                    getUserRoles().remove(userRole);
                    break;
                }
            }
        }
        Log.debug("< removeRole : "
                + (getUserRoles() != null ? getUserRoles().size() : "null"));
    }

    public String getEmail()
    {
        return email;
    }

    public Long getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public String getPassword()
    {
        return password;
    }

    public String getUsername()
    {
        return username;
    }

    public Set<UserRole> getUserRoles()
    {
        return userRoles;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public void setUserRoles(Set<UserRole> userRoles)
    {
        this.userRoles = userRoles;
    }

}
